//
//  YSGNurseAppointmentConsumablesCell.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/4/11.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit


protocol YSGNurseAppointmentConsumablesCellDelegate: NSObjectProtocol {
    func pay(_ orderId: String,_ orderAmount: String)
}
    


class YSGNurseAppointmentConsumablesCell: UITableViewCell {
    
    
    weak var delegate: YSGNurseAppointmentConsumablesCellDelegate?
    @IBOutlet weak var sumMoneyBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var payButton: UIButton!
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var sumMoneyLabel: UILabel!
    @IBOutlet weak var bodyView: UIView!
    @IBOutlet weak var orderNoLabel: UILabel!
    
    var sumMoneyNumber : NSDecimalNumber?
    
    var orderId : String?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    @IBAction func payAction(_ sender: Any) {
        
        payButton.isUserInteractionEnabled = false
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            self.payButton?.isUserInteractionEnabled = true
        }
        
        delegate?.pay(orderId  ?? "", sumMoneyNumber?.stringValue ?? "0")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

    func configure(_ array : Array<YSGNurseAppointmentInfoConsumablessModel>){
        bodyView.subviews.forEach {$0.removeFromSuperview()}

        sumMoneyNumber = NSDecimalNumber(string: "0")
        
        for item in 0..<array.count{
        
            
            let number :NSDecimalNumber  = NSDecimalNumber(string: array[item].orderAmount ?? "0")
            
            sumMoneyNumber =  sumMoneyNumber?.adding(number)
            
            let label = UILabel(frame: CGRect(x: 0, y: item * 30 , width: 200, height: 20))
            
            let labelString = NSMutableAttributedString(
                string:(array[item].consumables ?? "") + "*" + (array[item].number ?? ""),
                attributes: [NSAttributedString.Key.font:
                    UIFont(name: "PingFang SC", size: 14.0)!])
            
            
            labelString.addAttribute(NSAttributedString.Key.foregroundColor,value:UIColor.warning, range: NSRange(location: (array[item].consumables?.count ?? 0) + 1,length: array[item].number?.count ?? 0))
            
            labelString.addAttribute(NSAttributedString.Key.foregroundColor,value:UIColor.textDark, range: NSRange(location: 0 ,length: (array[item].consumables?.count ?? 0) + 1))
            
            label.attributedText = labelString
            
            bodyView.addSubview(label)

        }
        
        
        
        let sumMoneyString = NSMutableAttributedString(
            string:("耗材价格：" + String(format: "%.2f", (sumMoneyNumber?.floatValue ?? 0) / 100.0) + "元") ,
            attributes: [NSAttributedString.Key.font:
                UIFont(name: "PingFangSC-Semibold", size: 14.0)!])
        sumMoneyString.addAttribute(NSAttributedString.Key.foregroundColor,value:UIColor.warning, range: NSRange(location: 5,length: String(format: "%.2f", (sumMoneyNumber?.floatValue ?? 0) / 100.0).count))
        
        sumMoneyLabel.attributedText = sumMoneyString
        orderId = (array.first?.orderId ?? "")
        orderNoLabel.text = "订单编号: " + orderId!
  
        
        
        
        switch array.first?.orderStatus {
        case .unpaid:
            statusLabel.isHidden = true
            payButton.isHidden = false
            sumMoneyBottomConstraint.constant = 56
        default:
            statusLabel.isHidden = false
            statusLabel.text = "已支付"
            payButton.isHidden = true
            sumMoneyBottomConstraint.constant = 18
        }
        
        
    }
    
}
